package com.kejiguan.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.kejiguan.ui.R;
import com.kejiguan.ui.adapter.AddUserDialogMenuAdapter;
import com.kejiguan.ui.model.MenuItem;
import com.kejiguan.ui.model.UserManagerModer;
import com.kejiguan.ui.util.MD5Encrypt;

import java.util.List;

/**
 * Created by YYY on 2016/5/1.
 */
public class ModifyUserDialog extends Dialog implements View.OnClickListener{

    private Context mContext;
    private View mDetailView;
    private EditText nameInput,passwordInput,emailInput,telInput;
    private RecyclerView menuList;
    private AddUserDialogMenuAdapter menuAdapter;
    public Button addBtn,cancelBtn;
    private String userName,userPassword,userEmail,userTel,navList;

    private OnUserModifyListener userModifyListener;

    private UserManagerModer preUserManagerModer;

    public ModifyUserDialog(Context context) {
        super(context);
        init(context);
    }

    public ModifyUserDialog(Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    protected ModifyUserDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }


    private void init(Context context) {
        mContext=context;
        mDetailView = View.inflate(context, R.layout.dialog_modify_user_layout, null);

        nameInput = (EditText) mDetailView.findViewById(R.id.name_input);
        passwordInput = (EditText) mDetailView.findViewById(R.id.password_input);
        emailInput = (EditText) mDetailView.findViewById(R.id.email_input);
        telInput = (EditText) mDetailView.findViewById(R.id.tel_input);
        menuList= (RecyclerView) mDetailView.findViewById(R.id.menu_list);
        addBtn=(Button)mDetailView.findViewById(R.id.add_user_confirm_btn);
        cancelBtn=(Button)mDetailView.findViewById(R.id.add_user_cancel_btn);
        addBtn.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);

        GridLayoutManager gridLayoutManager=new GridLayoutManager(context,3);
        menuList.setLayoutManager(gridLayoutManager);
        setContentView(mDetailView);
        setCanceledOnTouchOutside(true);
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
        setCanceledOnTouchOutside(true);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.add_user_confirm_btn:
                userName=nameInput.getText().toString();
                userPassword=passwordInput.getText().toString();
                userEmail=emailInput.getText().toString();
                userTel=telInput.getText().toString();
                navList=menuAdapter.getSelectMenus();

                if (TextUtils.isEmpty(userName))
                {
                    break;
                }
                if (TextUtils.isEmpty(userEmail))
                {
                    break;
                }
                if (TextUtils.isEmpty(userTel))
                {
                    break;
                }
                if (TextUtils.isEmpty(navList))
                {
                    break;
                }

                if (!userName.equals(preUserManagerModer.getUserName())||
                        !TextUtils.isEmpty(userPassword)||
                        !userEmail.equals(preUserManagerModer.getEmail())||
                        !userTel.equals(preUserManagerModer.getTelephone())||
                        !navList.equals(preUserManagerModer.getNavList()))
                {
                    UserManagerModer userManagerModer=new UserManagerModer();
                    userManagerModer.setUserName(userName);
                    if (!TextUtils.isEmpty(userPassword))
                    {
                        userManagerModer.setPassword(MD5Encrypt.MD5Encode(userPassword));
                    }else {
                        userManagerModer.setPassword(preUserManagerModer.getPassword());
                    }
                    userManagerModer.setEmail(userEmail);
                    userManagerModer.setTelephone(userTel);
                    userManagerModer.setNavList(navList);
                    userManagerModer.setUserId(preUserManagerModer.getUserId());

                    if (null!=userModifyListener)
                    {
                        userModifyListener.onUserModify(userManagerModer);
                    }
                }


                dismiss();
                break;
            case R.id.add_user_cancel_btn:
                dismiss();
                break;
        }
    }

    public ModifyUserDialog withUser(UserManagerModer userManagerModer)
    {
        this.preUserManagerModer=userManagerModer;
        nameInput.setText(preUserManagerModer.getUserName());
        telInput.setText(preUserManagerModer.getTelephone());
        emailInput.setText(preUserManagerModer.getEmail());
        menuAdapter.setHasSelctedMenus(userManagerModer.getNavList());
        return this;
    }

    public ModifyUserDialog withMenus(List<MenuItem> menuItems)
    {
        menuAdapter=new AddUserDialogMenuAdapter(mContext,menuItems);
        menuList.setAdapter(menuAdapter);
        return this;
    }

    public ModifyUserDialog withOnUserModifyListener(OnUserModifyListener userModifyListener)
    {
        this.userModifyListener=userModifyListener;
        return this;
    }


    public interface OnUserModifyListener
    {
        public void onUserModify(UserManagerModer userManagerModer);
    }
}
